const request = require('request')
const cheerio = require('cheerio')
const fs = require('fs')
const fileName = 'data_'+Date.now()+'.json'
let fileContent = ''

let options = {
    url: 'https://zhuanlan.zhihu.com/p/114503214',
    method: 'GET',
    charset: "utf-8",
    headers: {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36",
        "cookie": 'q_c1=0c84fab3dc9242bc9a91c26000058226|1578991237000|1577157440; _zap=1e7708f9-0034-4114-9528-d030c0186e32; d_c0="AEAhQNaNjRCPTg1nzdNz-JdYXkr4-W5IMgI=|1577157438"; _ga=GA1.2.1660455762.1582712386; tst=r; __utma=155987696.1660455762.1582712386.1590485565.1590485565.1; __utmz=155987696.1590485565.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); z_c0=Mi4xYVlBUEJnQUFBQUFBUUNGQTFvMk5FQmNBQUFCaEFsVk5lalFfWUFDSG9BM09ybnZldHhUSW9SaGFxVEI2QXZicjdn|1599202938|e9bec2b45a68487b6bf92dc481af9a804840733a; q_c1=0de591395e7e43ad973b8a9d7e45dc0c|1604989006000|1583132017000; _xsrf=50eb2959-8dfd-4c67-8975-70f050dd041a; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1605593659,1605594019,1605599880,1605667670; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1605667670; KLBRSID=9d75f80756f65c61b0a50d80b4ca9b13|1605667842|1605667647'
    }
};

request(options,function(err,result){
    if(err){
        console.log(err);
    }
    let $ = cheerio.load(result.body);
    $('.Post-RichTextContainer p').each(function(index,element){
        let _el = $(element).text();
        if((_el.indexOf('日记') === -1 ) && (_el)) {
            fileContent += '{\n"content": "'+$(element).text()+'"\n}\n'
        }
    })
    fs.open(fileName, 'w+', (err, fd) => {
        if (err) {
            console.error(err);
        } else {
            console.log(fileContent)
            fs.writeFile(fileName, fileContent, () => {

            })
        }
    })
})
